package com.lc.app.androidfirst.userdefine.activity;

import android.app.Activity;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lc.app.androidfirst.R;

public class GetContactsActivity extends AppCompatActivity {

    private EditText testContactName;
    private EditText testContactPhone;

    private static final int PICK_CONTACT = 0x12;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_contacts);

        testContactName = (EditText) findViewById(R.id.testContactName);
        testContactPhone = (EditText) findViewById(R.id.testContactPhone);

        Button btnTestContanctShow = (Button) findViewById(R.id.testContactShow);
        btnTestContanctShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.setType("vnd.android.cursor.item/phone");
                startActivityForResult(intent,PICK_CONTACT);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case PICK_CONTACT:
                if(resultCode == Activity.RESULT_OK){
                    Uri contactData = data.getData();
                    Cursor cursor = getContentResolver().query(contactData,null,null,null,null);
                    if(cursor.moveToFirst()){
                        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                        String phoneNumber = "此联系人暂未输入手机号码";
                        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId,null,null);
                        if(phones.moveToFirst()){
                            phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        }
                        if (Build.VERSION.SDK_INT < 14) {
                            phones.close();
                        }
                        testContactName.setText(name);
                        testContactPhone.setText(phoneNumber);
                    }
                    if (Build.VERSION.SDK_INT < 14) {
                        cursor.close();
                    }

                }

        }
    }
}
